package com.qjkji.orderproject.core.controller;


import com.qjkji.orderproject.core.entity.Result;
import com.qjkji.orderproject.core.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
@Api(tags = "路由")
@Slf4j
public class IndexController {

    @Resource
    AuthenticationManager authenticationManager;

    @PostMapping(value = "/login")
    @ResponseBody
    public Result login(HttpServletRequest request,
                            @RequestParam("username") String username,
                            @RequestParam("password") String password) {

        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(username, password);
        try{
            //使用SpringSecurity拦截登陆请求 进行认证和授权
            Authentication authenticate = authenticationManager.authenticate(usernamePasswordAuthenticationToken);
            SecurityContextHolder.getContext().setAuthentication(authenticate);
            HttpSession session = request.getSession();
            session.setAttribute("SPRING_SECURITY_CONTEXT", SecurityContextHolder.getContext()); // 这个非常重要，否则验证后将无法登陆
        }catch (Exception e){
            return Result.error("登陆失败!");
        }

        return Result.success("登陆成功！");
    }

    @RequestMapping("/")
    public String index(){
        String userName= SecurityUtil.getUserName();

        if(userName == null || userName.trim().isEmpty() || userName.equals("anonymousUser")){
            return "login";
        }
        return "frontManager";
    }

    @RequestMapping("/front")
    @ApiOperation("前台管理")
    public String front(){
        String userName= SecurityUtil.getUserName();

        if(userName == null || userName.trim().isEmpty() || userName.equals("anonymousUser")){
            return "login";
        }
        return "frontManager";
    }

    @RequestMapping("/food")
    @ApiOperation("菜品管理")
    public String food(){
        String userName= SecurityUtil.getUserName();


        if(userName == null || userName.trim().isEmpty() || userName.equals("anonymousUser")){
            return "login";
        }
        return "foodManager";
    }

    @RequestMapping("/reservation")
    @ApiOperation("预约管理")
    public String reservation(){
        String userName= SecurityUtil.getUserName();


        if(userName == null || userName.trim().isEmpty() || userName.equals("anonymousUser")){
            return "login";
        }
        return "reservationManager";
    }

    @RequestMapping("/order")
    @ApiOperation("订单管理")
    public String order(){
        String userName= SecurityUtil.getUserName();


        if(userName == null || userName.trim().isEmpty() || userName.equals("anonymousUser")){
            return "login";
        }
        return "orderManager";
    }

    @RequestMapping("/myinfo")
    @ApiOperation("我的信息")
    public String myinfo(){
        String userName= SecurityUtil.getUserName();


        if(userName == null || userName.trim().isEmpty() || userName.equals("anonymousUser")){
            return "login";
        }
        return "myinfo";
    }
}
